<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1><span class="color_h1">javascript:void(0)</span> 含义
</h1>
<p class="intro">我们经常会使用到 <span class="marked">javascript:void(0)</span> 这样的代码，那么在 JavaScript 中 <span class="marked">javascript:void(0)</span> 代表的是什么意思呢？</p>
<p class="intro"><span class="marked">javascript:void(0)</span> 中最关键的是 <strong>void</strong> 关键字，
<strong>void</strong> 是 JavaScript 中非常重要的关键字，该操作符指定要计算一个表达式但是不返回值。
</p>
<p>语法格式如下：</p>
<pre class="prettyprint prettyprinted" style=""><span class="kwd">void</span><span class="pln"> func</span><span class="pun">()</span><span class="pln">
javascript</span><span class="pun">:</span><span class="kwd">void</span><span class="pln"> func</span><span class="pun">()</span><button class="copy-code-button" type="button" data-clipboard-text="void func()
javascript:void func()
"></button></pre>
<p>或者</p>
<pre class="prettyprint prettyprinted" style=""><span class="kwd">void</span><span class="pun">(</span><span class="pln">func</span><span class="pun">())</span><span class="pln">
javascript</span><span class="pun">:</span><span class="kwd">void</span><span class="pun">(</span><span class="pln">func</span><span class="pun">())</span><button class="copy-code-button" type="button" data-clipboard-text="void(func())
javascript:void(func())
"></button></pre>
<p>下面的代码创建了一个超级链接，当用户点击以后不会发生任何事。</p>
<div class="example"> <h2 class="example">实例</h2> <div class="example_code"> 
<div class="hl-main"><span class="hl-code">&lt;</span><span class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-identifier">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">javascript:void(0)</span><span class="hl-quotes">"</span><span class="hl-code">&gt;单击此处什么也不会发生&lt;/</span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="<a href=&quot;javascript:void(0)&quot;>单击此处什么也不会发生</a>"></button></div> <br><a class="tryitbtn" href="/try/try.php?filename=tryjs_void&amp;basepath=0" target="_blank" rel="noopener noreferrer">尝试一下 »</a> </div>
<p>当用户链接时，void(0) 计算为 0，但 Javascript 上没有任何效果。</p>

<p>以下实例中，在用户点击链接后显示警告信息：</p>
<div class="example"> <h2 class="example">实例</h2> <div class="example_code"> 
<div class="hl-main"><span class="hl-code">&lt;</span><span class="hl-identifier">p</span><span class="hl-code">&gt;点击以下链接查看结果：&lt;/</span><span class="hl-identifier">p</span><span class="hl-code">&gt;
&lt;</span><span class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-identifier">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">javascript:void(alert('Warning!!!'))</span><span class="hl-quotes">"</span><span class="hl-code">&gt;点我!&lt;/</span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="<p>点击以下链接查看结果：</p>
<a href=&quot;javascript:void(alert('Warning!!!'))&quot;>点我!</a>"></button></div> <br><a class="tryitbtn" href="/try/try.php?filename=tryjs_void1&amp;basepath=0" target="_blank" rel="noopener noreferrer">尝试一下 »</a> </div>
<p>以下实例中参数 a 将返回 undefined :</p>
<div class="example"> <h2 class="example">实例</h2> <div class="example_code"> 
<div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">getValue</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-code">,</span><span class="hl-identifier">c</span><span class="hl-code">;
   </span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-reserved">void</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">7</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">write</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">a = </span><span class="hl-quotes">'</span><span class="hl-code"> + </span><span class="hl-identifier">a</span><span class="hl-code"> + </span><span class="hl-quotes">'</span><span class="hl-string"> b = </span><span class="hl-quotes">'</span><span class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-code"> +</span><span class="hl-quotes">'</span><span class="hl-string"> c = </span><span class="hl-quotes">'</span><span class="hl-code"> + </span><span class="hl-identifier">c</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="function getValue(){
   var a,b,c;
   a = void ( b = 5, c = 7 );
   document.write('a = ' + a + ' b = ' + b +' c = ' + c );
}"></button></div> <br><a class="tryitbtn" href="/try/try.php?filename=tryjs_void2&amp;basepath=0" target="_blank" rel="noopener noreferrer">尝试一下 »</a> </div>
<hr>
<h2>href="#"与href="javascript:void(0)"的区别</h2>
<p><strong>#</strong> 包含了一个位置信息，默认的锚是<strong>#top</strong> 也就是网页的上端。</p>
<p> 而javascript:void(0), 仅仅表示一个死链接。</p>
<p>在页面很长的时候会使用 <strong>#</strong> 来定位页面的具体位置，格式为：<strong># + id</strong>。</p>
<p>如果你要定义一个死链接请使用 javascript:void(0) 。
</p>

<div class="example"> <h2 class="example">实例</h2> <div class="example_code"> 
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">a</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">javascript:void(0);</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点我没有反应的!</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">a</span><span class="hl-code"> </span><span class="hl-var">href</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">#pos</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点我定位到指定位置!</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">a</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">br</span><span class="hl-brackets">&gt;</span><span class="hl-code">
...
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">br</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-code"> </span><span class="hl-var">id</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">pos</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">尾部定位点</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span></div>
<button class="copy-code-button" type="button" data-clipboard-text="<a href=&quot;javascript:void(0);&quot;>点我没有反应的!</a>
<a href=&quot;#pos&quot;>点我定位到指定位置!</a>
<br>
...
<br>
<p id=&quot;pos&quot;>尾部定位点</p>"></button></div> <br><a class="tryitbtn" href="/try/demo_source/tryjs_void3.htm" target="_blank" rel="noopener noreferrer">尝试一下 »</a> </div>			<!-- 其他扩展 -->
						
			</div>
			
		</div>